package com.nhim.core;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Repository;

@Repository
public class AppSettings {
	private final String TEMPLATE_DIRECTORY_NAME = "Templates";
	
	private String assets;
	private String encoding;
	private Date maxDate;
	
	@Value("${com.nhim.cms.encoding}")
	public void setEncoding(String encoding) {
		this.encoding = encoding;
	}
	
	public String getEncoding()
	{
		return this.encoding;
	}
	public String getAssetsPath()
	{
		return this.assets;
	}
	
	@Value("${com.nhim.cms.assetpath}")
	public void setAssetsPath(String assets) {
		this.assets = assets;
	}
	public String getTemplatesPath()
	{
		return getAssetsPath() + TEMPLATE_DIRECTORY_NAME + "/" ;
	}
	@Value("${com.nhim.cms.maxdate}")
	public void setMaxDate(String maxDate) throws ParseException
	{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd"); 
		this.maxDate = dateFormat.parse(maxDate);
	}
	public Date getMaxDate() 
	{
		return this.maxDate;
	}
	
	
	public static AppSettings getFromApplicationContext(
			ApplicationContext ctx) {
		return (AppSettings) ctx.getBean(AppSettings.class);
	}
	public static AppSettings getInstance()
	{
		return (AppSettings) AppContext.getApplicationContext()
		.getBean(AppSettings.class);
	}

}
